//
//  AddViewController.m
//  17-homework-addressBook
//
//  Created by 王道林 on 16/8/8.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "AddViewController.h"

@interface AddViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *TextfieldName;
@property (weak, nonatomic) IBOutlet UITextField *TextFieldPhone;
@property (weak, nonatomic) IBOutlet UITextField *TextfieldPhone;
@property (weak, nonatomic) IBOutlet UITextField *TextFieldAge;
@property (weak, nonatomic) IBOutlet UITextField *TextFieldHome;
@property (weak, nonatomic) IBOutlet UIImageView *iamgeview;
@property (nonatomic, strong) Contecter *contecter;
@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createTap];
}
- (IBAction)AddContecter:(id)sender {
    
    _contecter = [[Contecter alloc] initWithName:_TextfieldName.text PicName:@"2" phoneNumber:_TextFieldPhone.text homePlace:_TextFieldHome.text];
    _contecter.age = _TextFieldAge.text;
    _contecter.gender = _TextfieldPhone.text;
    
    [self.delegate passValue:_contecter];
    [self.navigationController popViewControllerAnimated:YES];
    
    
}

- (void)createTap
{
    self.iamgeview.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePhoto:)];
    [self.iamgeview addGestureRecognizer:tap];
}

- (void)handlePhoto:(UITapGestureRecognizer *)tap
{
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    //    UIImagePickerControllerSourceType *sourceType = UIImagePickerControllerCameraCaptureModePhoto;
    //    picker.sourceType = sourceType;
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
}


- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    self.iamgeview.image = image;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    [picker dismissViewControllerAnimated:YES completion:^{
        self.iamgeview.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
